﻿#include "gamemanager.h"
#include "dialogask.h"
#include "gamewin.h"

#include <QDebug>
#include <cmath>
#include <random>
#include <QTimer>
#include <QPair>

GameManager::GameManager(GameSce* scene, QList<Card*>* list, QObject *parent):
    QObject(parent),
    m_sceneGame(scene),
    m_listCard(list)
{
    init();
}

GameManager::~GameManager()
{

}

void GameManager::init()
{
    // 初始化播放器
    m_doPlay = new QMediaPlayer(this);
    m_doPlay->setMedia(QUrl("qrc:/Monster_Kill/Res/Sound/CardDo.mp3"));
    m_doPlay->setVolume(100);

    m_overPlay = new QMediaPlayer(this);
    m_overPlay->setMedia(QUrl("qrc:/Monster_Kill/Res/Sound/GameOver.mp3"));
    m_overPlay->setVolume(100);

    // 初始化信号槽
    m_step = 0;
    for(int i = 0; i < m_listCard->count(); i++){
        connect(m_listCard->at(i), &Card::cardDO, this, &GameManager::Do);
    }

    // 初始化专门用于计算价值的二维数组
    for(int i = 0; i < m_listCard->count(); i++){
        int row = i / 4;
        int col = i % 4;
        cards_info[row][col].group = m_listCard->at(i)->m_group ? Card::RED : Card::BLUE;
        cards_info[row][col].type = m_listCard->at(i)->m_type;
        cards_info[row][col].isBack = m_listCard->at(i)->m_isBack;
    }

    // 连接必要的信号和槽，使得一方棋手在完成下棋后，可以立刻调回给另一方棋手权力
    connect(this, &GameManager::signal_humanOver,[this](){
        refuse();
        QTimer* timer = new QTimer();
        timer->start(2000);
        timer->setSingleShot(true);
        connect(timer, &QTimer::timeout, this, &GameManager::algorithm_akgry_main);
    });
    connect(this, &GameManager::signal_aiOver, this, &GameManager::recover);
}

void GameManager::HumanDo(bool humanGroup, int Rotation, Card* card)
{

    // 玩家总是默认第一次开牌
    if(m_step == 0){ // 玩家第一次开牌
        m_HumanGroup = humanGroup == Card::BLUE ? Card::BLUE : Card::RED;
        m_AiGroup = humanGroup == Card::BLUE ? Card::RED : Card::BLUE;
    }

    m_step++;
    // 中间执行一些列更新棋盘的操作
    int index = m_listCard->indexOf(card); // 该接口是正常调用的
    int row = index / 4;
    int col = index % 4;
    Card* tempCard;

    switch (Rotation) {
    case 0: // 表明当前的操作是开牌
        cards_info[row][col].isBack = false;
        break;
    case 1:
        m_listCard->at(index - 4)->setVisible(false); // 令上方的卡牌不可见
        m_listCard->at(index - 4)->m_group = Card::DEAD; // 令上方的卡牌阵亡
        if(cards_info[row - 1][col].type == cards_info[row][col].type){ // 同归于尽的操作
            m_listCard->at(index)->setVisible(false); // 令当前的卡牌不可见
            m_listCard->at(index)->m_group = Card::DEAD; // 令当前的卡牌阵亡
        }

        tempCard = m_listCard->operator[](index); // tempCard赋值为中心卡
        m_listCard->operator[](index) = m_listCard->at(index - 4); // 中心卡换位为上方卡
        m_listCard->operator[](index - 4) = tempCard; // 上方卡换位为中心卡

        if(cards_info[row - 1][col].type == cards_info[row][col].type){ // 同归于尽的操作
            cards_info[row - 1][col].type = Card::NU;
            cards_info[row - 1][col].group = Card::DEAD;
        }else{
            cards_info[row - 1][col].type = cards_info[row][col].type;
            cards_info[row - 1][col].group = cards_info[row][col].group;
        }
        cards_info[row][col].type = Card::NU;
        cards_info[row][col].group = Card::DEAD;
        break;
    case 2:
        m_listCard->at(index + 4)->setVisible(false);
        m_listCard->at(index + 4)->m_group = Card::DEAD;
        if(cards_info[row + 1][col].type == cards_info[row][col].type){ // 同归于尽的操作
            m_listCard->at(index)->setVisible(false); // 令当前的卡牌不可见
            m_listCard->at(index)->m_group = Card::DEAD; // 令当前的卡牌阵亡
        }

        tempCard = m_listCard->operator[](index);
        m_listCard->operator[](index) = m_listCard->at(index + 4);
        m_listCard->operator[](index + 4) = tempCard;

        if(cards_info[row + 1][col].type == cards_info[row][col].type){ // 同归于尽的操作
            cards_info[row + 1][col].type = Card::NU;
            cards_info[row + 1][col].group = Card::DEAD;
        }else{
            cards_info[row + 1][col].type = cards_info[row][col].type;
            cards_info[row + 1][col].group = cards_info[row][col].group;
        }
        cards_info[row][col].type = Card::NU;
        cards_info[row][col].group = Card::DEAD;
        break;
    case 3:
        m_listCard->at(index - 1)->setVisible(false);
        m_listCard->at(index - 1)->m_group = Card::DEAD;
        if(cards_info[row][col - 1].type == cards_info[row][col].type){ // 同归于尽的操作
            m_listCard->at(index)->setVisible(false); // 令当前的卡牌不可见
            m_listCard->at(index)->m_group = Card::DEAD; // 令当前的卡牌阵亡
        }

        tempCard = m_listCard->operator[](index);
        m_listCard->operator[](index) = m_listCard->at(index - 1);
        m_listCard->operator[](index - 1) = tempCard;

        if(cards_info[row][col - 1].type == cards_info[row][col].type){ // 同归于尽的操作
            cards_info[row][col - 1].type = Card::NU;
            cards_info[row][col - 1].group = Card::DEAD;
        }else{
            cards_info[row][col - 1].type = cards_info[row][col].type;
            cards_info[row][col - 1].group = cards_info[row][col].group;
        }
        cards_info[row][col].type = Card::NU;
        cards_info[row][col].group = Card::DEAD;
        break;
    case 4:
        m_listCard->at(index + 1)->setVisible(false);
        m_listCard->at(index + 1)->m_group = Card::DEAD;
        if(cards_info[row][col + 1].type == cards_info[row][col].type){ // 同归于尽的操作
            m_listCard->at(index)->setVisible(false); // 令当前的卡牌不可见
            m_listCard->at(index)->m_group = Card::DEAD; // 令当前的卡牌阵亡
        }

        tempCard = m_listCard->operator[](index);
        m_listCard->operator[](index) = m_listCard->at(index + 1);
        m_listCard->operator[](index + 1) = tempCard;

        if(cards_info[row][col + 1].type == cards_info[row][col].type){ // 同归于尽的操作
            cards_info[row][col + 1].type = Card::NU;
            cards_info[row][col + 1].group = Card::DEAD;
        }else{
            cards_info[row][col + 1].type = cards_info[row][col].type;
            cards_info[row][col + 1].group = cards_info[row][col].group;
        }
        cards_info[row][col].type = Card::NU;
        cards_info[row][col].group = Card::DEAD;
        break;
    default:
        break;
    }

    //qDebug() << "玩家操作完毕" << endl;
    emit signal_humanOver(m_AiGroup);
}

void GameManager::AiDo()
{
    m_step++;
    QTimer* timer = new QTimer();
    timer->start(1000);
    timer->setSingleShot(true);
    connect(timer, &QTimer::timeout, this, [this](){
        //qDebug() << "电脑操作完毕" << endl;
        emit signal_aiOver(m_HumanGroup);
    });

}

// 逃脱算法
int GameManager::algorithm_akgry_escape(int rotation, int row, int col)
{
    // 赋值对应卡牌
    Card_Info upCard = cards_info[row - 1][col];
    Card_Info downCard = cards_info[row + 1][col];
    Card_Info leftCard = cards_info[row][col - 1];
    Card_Info rightCard = cards_info[row][col + 1];
    Card_Info centerCard = cards_info[row][col];

    // 开始判断, 根据威胁的方向判断其它方向, 只要找到逃跑通道, 就可以立刻沿该通道进行撤离
    switch (rotation) {
    // 威胁来自上方
    // 需要判断卡牌是否处于墙态
    case 1:
        // 首先判断下方
        if(downCard.isBack == false || downCard.isWall == true){
            if(downCard.group != m_AiGroup && downCard.type < centerCard.type && row < 3){
                return 2;
            }
        }
        // 判断左侧
        if(leftCard.isBack == false || leftCard.isWall == true){
            if(leftCard.group != m_AiGroup && leftCard.type < centerCard.type && col > 0){
                return 3;
            }
        }
        // 判断右侧
        if(rightCard.isBack == false || rightCard.isWall == true){
            if(rightCard.group != m_AiGroup && rightCard.type < centerCard.type && col < 3){
                return 4;
            }
        }
        // 避无可避
        if(upCard.type == centerCard.type){
            return 5; // 无路可走，但是能够同归于尽
        }else{
            return 9; // 无路可走，并且死路一条
        }

        break;

    // 威胁来自下方
    case 2:
        // 首先判断上方
        if(upCard.isBack == false || upCard.isWall == true){
            if(upCard.group != m_AiGroup && upCard.type < centerCard.type && row > 0){
                return 1;
            }
        }
        // 判断左侧
        if(leftCard.isBack == false || leftCard.isWall == true){
            if(leftCard.group != m_AiGroup && leftCard.type < centerCard.type && col > 0){
                return 3;
            }
        }
        // 判断右侧
        if(rightCard.isBack == false || rightCard.isWall == true){
            if(rightCard.group != m_AiGroup && rightCard.type < centerCard.type && col < 3){
                return 4;
            }
        }
        // 避无可避
        if(downCard.type == centerCard.type){
            return 6; // 无路可走，但是能够同归于尽
        }else{
            return 9; // 无路可走，并且死路一条
        }

        break;
    // 威胁来自左侧
    case 3:
        // 首先判断上方
        if(upCard.isBack == false || upCard.isWall == true){
            if(upCard.group != m_AiGroup && upCard.type < centerCard.type && row > 0){
                return 1;
            }
        }
        // 判断下方
        if(downCard.isBack == false || downCard.isWall == true){
            if(downCard.group != m_AiGroup && downCard.type < centerCard.type && row < 3){
                return 2;
            }
        }
        // 判断右侧
        if(rightCard.isBack == false || rightCard.isWall == true){
            if(rightCard.group != m_AiGroup && rightCard.type < centerCard.type && col < 3){
                return 4;
            }
        }
        // 避无可避
        if(leftCard.type == centerCard.type){
            return 7; // 无路可走，但是能够同归于尽
        }else{
            return 9; // 无路可走，并且死路一条
        }

        break;
    // 威胁来自右侧
    case 4:
        // 首先判断上方
        if(upCard.isBack == false || upCard.isWall == true){
            if(upCard.group != m_AiGroup && upCard.type < centerCard.type && row > 0){
                return 1;
            }
        }
        // 判断下方
        if(downCard.isBack == false || downCard.isWall == true){
            if(downCard.group != m_AiGroup && downCard.type < centerCard.type && row < 3){
                return 2;
            }
        }
        // 判断左侧
        if(leftCard.isBack == false || leftCard.isWall == true){
            if(leftCard.group != m_AiGroup && leftCard.type < centerCard.type && col > 0){
                return 3;
            }
        }
        // 避无可避
        if(rightCard.type == centerCard.type){
            return 8; // 无路可走，但是能够同归于尽
        }else{
            return 9; // 无路可走，并且死路一条
        }
        break;
    default:
        break;
    }

    return 999;  // 过编译用的，一般情况下不会走到这一步
}

void GameManager::algorithm_akgry_do(int para, int tRow, int tCol)
{
    int index = tRow * 4 + tCol;
    Card* tempCard;
    QList<Card*> tempList;
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); // 生成种子
    std::default_random_engine generator(seed); // 创建随机数引擎;
    std::uniform_int_distribution<int> distribution(0, 100);
    int random_int = distribution(generator);

    // 第九种情况的时候，需要额外定义的局部变量
    int maxValue_9 = 0;
    int maxRotation_9 = 0;
    pos maxPos_9; maxPos_9.row = 0; maxPos_9.col = 0;

    switch (para) {
    case 0: // 随机开牌
        for(int row = 0; row < 4; row++){
            for(int col = 0; col < 4; col++){
                // 确保卡牌是翻面的，且不能是墙态
                if(cards_info[row][col].isBack == true && cards_info[row][col].isWall == false){
                    int index = row * 4 + col;
                    tempList.append(m_listCard->at(index));
                }
            }
        }
        // 打乱tempList中的所有卡牌顺序，然后挑选第一张

        std::shuffle(tempList.begin(), tempList.end(), generator);

        if(tempList.count() > 0 && random_int < 68){
            tempList[0]->openAnimation();
            int tempindex = m_listCard->indexOf(tempList[0]);
            int tempRow = tempindex / 4;
            int tempCol = tempindex % 4;
            cards_info[tempRow][tempCol].isBack = false;
        }else{ // 进行随机移动
            // 开始进行战略行动
            bool noCard = tempList.count() > 0 ? false : true;
            algorithm_akgry_strategy(noCard);
        }


        break;
    case 1: // 上移
        m_listCard->at(index)->upAnimation();
        m_listCard->at(index - 4)->setVisible(false);
        m_listCard->at(index - 4)->m_group = Card::DEAD;

        tempCard = m_listCard->operator[](index);
        m_listCard->operator[](index) = m_listCard->at(index - 4);
        m_listCard->operator[](index - 4) = tempCard;

        cards_info[tRow - 1][tCol].type = cards_info[tRow][tCol].type;
        cards_info[tRow - 1][tCol].group = cards_info[tRow][tCol].group;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 2:
        m_listCard->at(index)->downAnimation();
        m_listCard->at(index + 4)->setVisible(false);
        m_listCard->at(index + 4)->m_group = Card::DEAD;

        tempCard = m_listCard->operator[](index);
        m_listCard->operator[](index) = m_listCard->at(index + 4);
        m_listCard->operator[](index + 4) = tempCard;

        cards_info[tRow + 1][tCol].type = cards_info[tRow][tCol].type;
        cards_info[tRow + 1][tCol].group = cards_info[tRow][tCol].group;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 3: // 左移
        m_listCard->at(index)->leftAnimation();
        m_listCard->at(index - 1)->setVisible(false);
        m_listCard->at(index - 1)->m_group = Card::DEAD;

        tempCard = m_listCard->operator[](index);
        m_listCard->operator[](index) = m_listCard->at(index - 1);
        m_listCard->operator[](index - 1) = tempCard;

        cards_info[tRow][tCol - 1].type = cards_info[tRow][tCol].type;
        cards_info[tRow][tCol - 1].group = cards_info[tRow][tCol].group;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 4: // 右移
        m_listCard->at(index)->rightAnimation();
        m_listCard->at(index + 1)->setVisible(false);
        m_listCard->at(index + 1)->m_group = Card::DEAD;

        tempCard = m_listCard->operator[](index);
        m_listCard->operator[](index) = m_listCard->at(index + 1);
        m_listCard->operator[](index + 1) = tempCard;

        cards_info[tRow][tCol + 1].type = cards_info[tRow][tCol].type;
        cards_info[tRow][tCol + 1].group = cards_info[tRow][tCol].group;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 5: // 决定与上方的对手同归于尽
        m_listCard->at(index)->upAnimation();
        m_listCard->at(index)->setVisible(false);
        m_listCard->at(index)->m_group = Card::DEAD;
        m_listCard->at(index - 4)->setVisible(false);
        m_listCard->at(index - 4)->m_group = Card::DEAD;

        cards_info[tRow - 1][tCol].type = Card::NU;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow - 1][tCol].group = Card::DEAD;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 6: // 决定与下方的对手同归于尽
        m_listCard->at(index)->downAnimation();
        m_listCard->at(index)->setVisible(false);
        m_listCard->at(index)->m_group = Card::DEAD;
        m_listCard->at(index + 4)->setVisible(false);
        m_listCard->at(index + 4)->m_group = Card::DEAD;

        cards_info[tRow + 1][tCol].type = Card::NU;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow + 1][tCol].group = Card::DEAD;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 7: // 决定与左侧的对手同归于尽
        m_listCard->at(index)->leftAnimation();
        m_listCard->at(index)->setVisible(false);
        m_listCard->at(index)->m_group = Card::DEAD;
        m_listCard->at(index - 1)->setVisible(false);
        m_listCard->at(index - 1)->m_group = Card::DEAD;

        cards_info[tRow][tCol - 1].type = Card::NU;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow][tCol - 1].group = Card::DEAD;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 8: // 决定与右侧的对手同归于尽
        m_listCard->at(index)->rightAnimation();
        m_listCard->at(index)->setVisible(false);
        m_listCard->at(index)->m_group = Card::DEAD;
        m_listCard->at(index + 1)->setVisible(false);
        m_listCard->at(index + 1)->m_group = Card::DEAD;

        cards_info[tRow][tCol + 1].type = Card::NU;
        cards_info[tRow][tCol].type = Card::NU;
        cards_info[tRow][tCol + 1].group = Card::DEAD;
        cards_info[tRow][tCol].group = Card::DEAD;
        break;
    case 9: // 死路一条，只能选择操纵其它棋子
        cards_info[tRow][tCol].isBack = true; // 先将其换化成一堵"墙"
        cards_info[tRow][tCol].isWall = true;

        algorithm_akgry_maxValue(maxValue_9, maxRotation_9, maxPos_9);
        if(maxValue_9 == 0){
            algorithm_akgry_do(0, 0, 0); // 直接开牌
        }else{
            if(maxValue_9 > 0){ // 直接开吃
                algorithm_akgry_do(maxRotation_9, maxPos_9.row, maxPos_9.col);
            }else{
                int esc = algorithm_akgry_escape(maxRotation_9, maxPos_9.row, maxPos_9.col);
                algorithm_akgry_do(esc, maxPos_9.row, maxPos_9.col);
            }
        }
        cards_info[tRow][tCol].isBack = false; // 执行完成后将墙壁恢复，等待审判
        cards_info[tRow][tCol].isWall = false;
        break;

    default:
        break;
    }
}

// 主算法
void GameManager::algorithm_akgry_main()
{
    // 检查游戏是否结束
    bool lose = true;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(cards_info[i][j].group == m_HumanGroup){
                lose = false;
                break;
            }
        }
    }
    if(lose){
        gameOver(m_HumanGroup);
        return;
    }

    // 模拟电脑操作
    //qDebug() << "电脑正在通过算法下棋" << endl;
    //m_listCard->at(0)->openAnimation();

    pos pos_maxCard; // 当前最有价值的己方卡牌
    pos_maxCard.row = 0; pos_maxCard.col = 0;
    int maxValue = 0; // 当前最大的价值点
    int maxRotation = 0; // 当前最大价值点最有价值的方向 1上、2下、3左、4右

    // 通过算法找到了目前价值最高的点位
    algorithm_akgry_maxValue(maxValue, maxRotation, pos_maxCard);

    // 开始决策该棋子怎么行动(吃掉对手、同归于尽、逃跑、开牌？)
    if(maxValue > 0){ // 最大价值是正数，表明对方即将损失一枚价值巨大的棋子，可以开吃了
        int tRow = pos_maxCard.row;
        int tCol = pos_maxCard.col;
        // 通过参数执行具体的下棋操作
        algorithm_akgry_do(maxRotation, tRow, tCol);

    }else if(maxValue < 0){ // 最大价值是负数，表明即将失去一枚含金量非常高的棋子，需要准备逃跑了
        int tRow = pos_maxCard.row;
        int tCol = pos_maxCard.col;
        int escRoad = algorithm_akgry_escape(maxRotation, tRow, tCol); // 找到了一个逃跑方向
        algorithm_akgry_do(escRoad, tRow, tCol);
    }else{
        algorithm_akgry_do(0, 0, 0); // 直接开牌
    }

    // 检查游戏是否结束
    lose = true;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(cards_info[i][j].group == m_HumanGroup){
                lose = false;
                break;
            }
        }
    }
    if(lose){
        gameOver(m_HumanGroup);
        return;
    }

}

void GameManager::algorithm_akgry_strategy(bool noCard)
{
    // 优先找到所有己方可移动的卡牌
    QList<QPair<Card*, int>> tempList;
    for(int row = 0; row < 4; row++){
        for(int col = 0; col < 4; col++){
            // 找到一张己方的卡牌
            if(cards_info[row][col].group == m_AiGroup && cards_info[row][col].isBack == false){
                for(int i = 1; i < 5; i++){
                    int rot = algorithm_akgry_escape(i, row, col);
                    if(rot != 9){
                        int index = row * 4 + col;
                        tempList.append(QPair<Card*, int>(m_listCard->at(index), rot));
                        break;
                    }
                }
            }
        }
    }
    // 打乱tempList中的所有卡牌顺序，然后挑选第一张
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); // 生成种子
    std::default_random_engine generator(seed); // 创建随机数引擎;
    std::shuffle(tempList.begin(), tempList.end(), generator);
    if(tempList.count() > 0){
        int rot = tempList[0].second;
        int row = m_listCard->indexOf(tempList[0].first) / 4;
        int col = m_listCard->indexOf(tempList[0].first) % 4;
        algorithm_akgry_do(rot, row, col);
    }else{
        if(!noCard){
            algorithm_akgry_do(0, 0, 0);
        }else{ // 电脑输了
            gameOver(m_AiGroup);
        }
    }


}

void GameManager::gameOver(Card::Group loser)
{
    dynamic_cast<GameWin*>(parent())->m_gameBgm->stop();
    refuse(); // 无论谁获胜，先对卡牌进行封印
    QString strWinner = loser == m_AiGroup ? QString("玩家获胜") : QString("电脑获胜");
    //strWinner += "，现在是否要退出游戏？";
    DialogAsk* dialogWin = new DialogAsk(strWinner);
    dialogWin->show();
    m_overPlay->play();
    //connect(dialogWin, &DialogAsk::OkBtn, this, &GameManager::signal_exit);

    //qDebug() << strLoser << endl;
}

// 估值算法
void GameManager::algorithm_akgry_maxValue(int& maxValue, int& maxRotation, pos& pos_maxCard)
{
    for(int row = 0; row < 4; row++){
        for(int col = 0; col < 4; col++){
            Card_Info tempCard = cards_info[row][col];
            if(tempCard.isBack || tempCard.group != m_AiGroup){
                continue;
            }

            // 找到了一张己方已经翻面的卡牌，然后开始分析这张卡牌的四个方向是否有卡牌
            // 1_判断上方
            int tempRow_up = row - 1;
            int tempCol_up = col;
            if(tempRow_up > -1 && tempRow_up < 4 && tempCol_up > -1 && tempCol_up < 4){ // 防止防溢
                Card_Info tempCard_up = cards_info[tempRow_up][tempCol_up];
                if(tempCard_up.isBack == false && tempCard_up.group == m_HumanGroup){ // 找到的这张牌是玩家已亮出的牌
                    // 计算上方位置的价值
                    int tempValue = tempCard.type > tempCard_up.type ? tempCard_up.type : -tempCard.type;

                    // 判断老鼠和大象的情况
                    if(tempCard.type == Card::RAT &&  tempCard_up.type == Card::ELEPHANT){
                        tempValue = Card::ELEPHANT;
                    }else if(tempCard.type == Card::ELEPHANT && tempCard_up.type == Card::RAT){
                        tempValue = -Card::ELEPHANT;
                    }

                    // 判断最大价值情况，如果价值更大，则记录该点位
                    if(abs(tempValue) > abs(maxValue)){
                        maxValue = tempValue;
                        pos_maxCard.row = row; pos_maxCard.col = col;
                        maxRotation = 1;
                    }
                }
            }

            // 2_判断下方
            int tempRow_down = row + 1;
            int tempCol_down = col;
            if(tempRow_down > -1 && tempRow_down < 4 && tempCol_down > -1 && tempCol_down < 4){
                Card_Info tempCard_down = cards_info[tempRow_down][tempCol_down];
                if(tempCard_down.isBack == false && tempCard_down.group == m_HumanGroup){
                    int tempValue = tempCard.type > tempCard_down.type ? tempCard_down.type : -tempCard.type;

                    if(tempCard.type == Card::RAT &&  tempCard_down.type == Card::ELEPHANT){
                        tempValue = Card::ELEPHANT;
                    }else if(tempCard.type == Card::ELEPHANT && tempCard_down.type == Card::RAT){
                        tempValue = -Card::ELEPHANT;
                    }

                    if(abs(tempValue) > abs(maxValue)){
                        maxValue = tempValue;
                        pos_maxCard.row = row; pos_maxCard.col = col;
                        maxRotation = 2;
                    }
                }
            }

            // 3_判断左方
            int tempRow_left = row;
            int tempCol_left = col - 1;
            if(tempRow_left > -1 && tempRow_left < 4 && tempCol_left > -1 && tempCol_left < 4){
                Card_Info tempCard_left = cards_info[tempRow_left][tempCol_left];
                if(tempCard_left.isBack == false && tempCard_left.group == m_HumanGroup){
                    int tempValue = tempCard.type > tempCard_left.type ? tempCard_left.type : -tempCard.type;

                    if(tempCard.type == Card::RAT &&  tempCard_left.type == Card::ELEPHANT){
                        tempValue = Card::ELEPHANT;
                    }else if(tempCard.type == Card::ELEPHANT && tempCard_left.type == Card::RAT){
                        tempValue = -Card::ELEPHANT;
                    }

                    if(abs(tempValue) > abs(maxValue)){
                        maxValue = tempValue;
                        pos_maxCard.row = row; pos_maxCard.col = col;
                        maxRotation = 3;
                    }
                }
            }

            // 4_判断右方
            int tempRow_right = row;
            int tempCol_right = col + 1;
            if(tempRow_right > -1 && tempRow_right < 4 && tempCol_right > -1 && tempCol_right < 4){
                Card_Info tempCard_right = cards_info[tempRow_right][tempCol_right];
                if(tempCard_right.isBack == false && tempCard_right.group == m_HumanGroup){
                    int tempValue = tempCard.type > tempCard_right.type ? tempCard_right.type : -tempCard.type;

                    if(tempCard.type == Card::RAT &&  tempCard_right.type == Card::ELEPHANT){
                        tempValue = Card::ELEPHANT;
                    }else if(tempCard.type == Card::ELEPHANT && tempCard_right.type == Card::RAT){
                        tempValue = -Card::ELEPHANT;
                    }

                    if(abs(tempValue) > abs(maxValue)){
                        maxValue = tempValue;
                        pos_maxCard.row = row; pos_maxCard.col = col;
                        maxRotation = 4;
                    }
                }
            }

            // 四个方向均判断完毕
        }
    }
}

void GameManager::refuse()
{
    for(int i = 0; i < m_listCard->count(); i++){
        m_listCard->at(i)->setAcceptHoverEvents(false);
    }
    for(int i = 0; i < m_listCard->count(); i++){
        m_listCard->at(i)->Card_Select->stop();
        m_listCard->at(i)->setScale(1);
    }
}

void GameManager::recover()
{
    // 将必要的棋子解除封印和石化
    for(int i = 0; i < m_listCard->count(); i++){
        if(m_listCard->at(i)->m_group == m_HumanGroup || m_listCard->at(i)->m_isBack == true){
            m_listCard->at(i)->setAcceptHoverEvents(true);
            m_listCard->at(i)->m_upAllow = true;
            m_listCard->at(i)->m_downAllow = true;
            m_listCard->at(i)->m_leftAllow = true;
            m_listCard->at(i)->m_rightAllow = true;
        }
    }

    // 对部分棋子进行有必要的石化(禁止其一些非法操作)
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            int index = i * 4 + j;
            if(cards_info[i][j].group == m_HumanGroup){
                // 判断上方
                if(i == 0 || cards_info[i - 1][j].type > cards_info[i][j].type
                        || cards_info[i - 1][j].group == m_HumanGroup
                        || cards_info[i - 1][j].isBack == true
                        || (cards_info[i - 1][j].type == Card::RAT && cards_info[i][j].type == Card::ELEPHANT) // 禁止大象攻击老鼠
                        ){

                    if(cards_info[i - 1][j].type == Card::ELEPHANT && cards_info[i][j].type == Card::RAT){ // 允许老鼠攻击大象
                        continue;
                    }else{
                        m_listCard->at(index)->m_upAllow = false;
                    }
                }
                // 判断下方
                if(i == 3 || cards_info[i + 1][j].type > cards_info[i][j].type
                        || cards_info[i + 1][j].group == m_HumanGroup
                        || cards_info[i + 1][j].isBack == true
                        || (cards_info[i + 1][j].type == Card::RAT && cards_info[i][j].type == Card::ELEPHANT) // 禁止大象攻击老鼠
                        ){

                    if(cards_info[i + 1][j].type == Card::ELEPHANT && cards_info[i][j].type == Card::RAT){ // 允许老鼠攻击大象
                        continue;
                    }else{
                        m_listCard->at(index)->m_downAllow = false;
                    }
                }
                // 判断左侧
                if(j == 0 || cards_info[i][j - 1].type > cards_info[i][j].type
                        || cards_info[i][j - 1].group == m_HumanGroup
                        || cards_info[i][j - 1].isBack == true
                        || (cards_info[i][j - 1].type == Card::RAT && cards_info[i][j].type == Card::ELEPHANT) // 禁止大象攻击老鼠
                        ){

                    if(cards_info[i][j - 1].type == Card::ELEPHANT && cards_info[i][j].type == Card::RAT){ // 允许老鼠攻击大象
                        continue;
                    }else{
                        m_listCard->at(index)->m_leftAllow = false;
                    }
                }
                // 判断右侧
                if(j == 3 || cards_info[i][j + 1].type > cards_info[i][j].type
                        || cards_info[i][j + 1].group == m_HumanGroup
                        || cards_info[i][j + 1].isBack == true
                        || (cards_info[i][j + 1].type == Card::RAT && cards_info[i][j].type == Card::ELEPHANT) // 禁止大象攻击老鼠
                        ){

                    if(cards_info[i][j + 1].type == Card::ELEPHANT && cards_info[i][j].type == Card::RAT){ // 允许老鼠攻击大象
                        continue;
                    }else{
                        m_listCard->at(index)->m_rightAllow = false;
                    }
                }
            }
        }
    }

}

void GameManager::Do(bool humanGroup, int Rotation, Card *card)
{

    m_doPlay->play();

    if(m_step % 2 == 0){
        HumanDo(humanGroup, Rotation, card);
    }else{
        AiDo();
    }
}

